package com.rbac.admin.controller;

import com.rbac.admin.common.Result;
import com.rbac.admin.service.CacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
/**
 * 缓存管理控制器
 */
@RestController
@RequestMapping("/api/cache")
@Tag(name = "缓存管理接口")
public class CacheController {

    @Autowired
    private CacheService cacheService;

    /**
     * 清除所有缓存
     */
    @DeleteMapping("/clear/all")
    @PreAuthorize("hasAuthority('sys:cache:clear')")
    @Operation(summary = "清除所有缓存")
    public Result<Void> clearAllCache() {
        cacheService.clearAll();
        return Result.success();
    }

    /**
     * 清除用户缓存
     */
    @DeleteMapping("/clear/user/{username}")
    @PreAuthorize("hasAuthority('sys:cache:clear')")
    @Operation(summary = "清除指定用户的缓存")
    public Result<Void> clearUserCache(@PathVariable String username) {
        cacheService.clearUserCache(username);
        return Result.success();
    }

    /**
     * 清除菜单缓存
     */
    @DeleteMapping("/clear/menu")
    @PreAuthorize("hasAuthority('sys:cache:clear')")
    @Operation(summary = "清除菜单缓存")
    public Result<Void> clearMenuCache() {
        cacheService.clearMenuCache();
        return Result.success();
    }

    /**
     * 清除权限缓存
     */
    @DeleteMapping("/clear/permission")
    @PreAuthorize("hasAuthority('sys:cache:clear')")
    @Operation(summary = "清除权限缓存")
    public Result<Void> clearPermissionCache() {
        cacheService.clearPermissionCache();
        return Result.success();
    }

    /**
     * 获取缓存统计信息
     */
    @GetMapping("/stats")
    @PreAuthorize("hasAuthority('sys:cache:view')")
    @Operation(summary = "获取缓存统计信息")
    public Result<Object> getCacheStats() {
        return Result.success(cacheService.getCacheStats());
    }
}